<?php
	use sinacloud\sae\Storage as Storage;
	require_once("request.php");
	$url = $_REQUEST['url'];
	echo $url;
	echo getSignature($url);
function getNewAccessToken(){
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9304369530ffc11d&secret=31977f083f40a72c9c2faa100c487ebb";
//请求链接  得到json串 包含access_token 和expires_in
	$response = httpGet($url);
	$res = json_decode($response);
	$access_token = $res->access_token;
	$expires_in = $res->expires_in;
	$current_time = time();
	$expires_time = $current_time + $expires_in -200;
	$array = (object)array();
	$array->access_token = $access_token;
	$array->expires_time = $expires_time;
	$saveStr = json_encode($array);
	$s = new Storage();
	 // 获取bucket
    $bucket = @$s->getBucket("bucket");
      // 如果bucket不存在 就创建bucket
      if ($bucket == false) {
        // 创建bucket
          $s->putBucket("bucket",".r:*");
      }
      // 写入操作
      $s->putObject($saveStr,"bucket","access_token.txt",array(),array("Content-type"=>"text/plain;charset=utf-8"));
      return $access_token;
}
// 获取已有的access_token
function readAccess_token(){
  // 先判断bucket中有没有存access_token
  // 如果存储了并且没有过期 直接使用
  // 如果过期了 就重新获取 并存储
 $s = new Storage();
 // 获取bucket  @符号取消用户警告
 $bucket = @$s->getBucket("bucket");
if ($bucket == false) {
    return false;
}else {
  $json = $s->getObject("bucket","access_token.txt");
  // 将json转换成$object返回   读取出的文件中有几个部分 其中内容部分的下标为body
  return json_decode($json->body);
  }
}
// 获取access_token
function getAccess_token(){
	$response = readAccess_token();
	// 判断本地有没有access_token
	if ($response) {
		// 如果storage中有access_token
		// 返回的是一个对象
		$access_token = $response->access_token;
		$expires_time = $response->expires_time;
		$current_time = time();
		if ($current_time > $expires_time) {
			$access_token = getNewAccessToken();
		}else{
			return $access_token;
		}
		return $access_token;
	}else{
// 如果storage中没有access_token
		return getNewAccessToken();
	}
}

// 获取ticket
function getNewJsapi_ticket(){
// 先获取access_token
	$access_token = getAccess_token();
	$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi";
	$response = httpGet($url);
	// 获取ticket
	$res = json_decode($response);
	$ticket = $res->ticket;
	$expires_in = $res->expires_in;
	$current_time = time();
	$expires_time = $current_time + $expires_in - 200;
	$array = (object)array();
	$array->ticket = $ticket;
	$array->expires_time = $expires_time;
	$saveStr = json_encode($array);
	$s = new Storage();
	 // 获取bucket
    $bucket = @$s->getBucket("bucket");
      // 如果bucket不存在 就创建bucket
      if ($bucket == false) {
        // 创建bucket
          $s->putBucket("bucket",".r:*");
      }
      // 写入操作
      $s->putObject($saveStr,"bucket","ticket.txt",array(),array("Content-type"=>"text/plain;charset=utf-8"));
      return $ticket;
}
// 读取本地的ticket
function  readTicket(){
$s = new Storage();
 // 获取bucket  @符号取消用户警告
 $bucket = @$s->getBucket("bucket");
if ($bucket == false) {
    return false;
}else {
  $json = $s->getObject("bucket","ticket.txt");
  // 将json转换成$object返回   读取出的文件中有几个部分 其中内容部分的下标为body
  return json_decode($json->body);
  }
}

function  getTicket(){
	// 先到storage中找
	$response = readTicket();
	if ($response) {
		$expires_time = $response->expires_time;
		$ticket = $response->ticket;
		$current_time = time();
		if ($current_time > $expires_time) {
			$ticket = getNewJsapi_ticket();
		}else{
			return $ticket;
		}
		return $ticket;
	}else{
		return  getNewJsapi_ticket();
	}
}
// 获取签名
function getSignature($url){
	$noncestr = createNonceStr();
	$jsapi_ticket = getTicket();
	$timestamp = time();
	$string = "jsapi_ticket={$jsapi_ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$url}";
	$newStr = sha1($string);
	$signPackage = array(
          "ticket"	  => $jsapi_ticket,
	      "appId"     => "wx9304369530ffc11d",
	      "nonceStr"  => $nonceStr,
	      "timestamp" => $timestamp,
	      "url"       => $url,
	      "signature" => $newStr,
	      "rawString" => $string
	    );
    return json_encode($signPackage);
}
echo getSignature($url);

// 生成随机字符串
function createNonceStr(){
	$temp = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  	$str = "";
  for ($i=0; $i < 16; $i++) {
  	$str .=  substr($temp,mt_rand(0,strlen($temp)-1),1);
  }
  return $str;
}




















 ?>
w